/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.amitycs.collabdrawing.gui;

import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import java.util.ArrayList;
import org.amitycs.collabdrawing.Whiteboard;

/**
 *
 * @author joe
 */
public class FreedrawTool extends Tool {
    private static final int SEGLENGTH = 5; // length of segments, in pixels

    private Line2D.Double currSeg; // current line segment

    public FreedrawTool(Whiteboard wb)
    {
        super(wb);
    }
    
    @Override
    public Shape onDrag(double x, double y) {
        currSeg.x2 = x;
        currSeg.y2 = y;
        if (Math.sqrt(Math.pow(currSeg.getX1() - x,2) + Math.pow(currSeg.getY1() - y, 2)) >= SEGLENGTH)
        {
            fireShapeDone(currSeg);
            // new segment time
            this.onPress(x, y);
        }
        return currSeg;
    }

    @Override
    public Shape onPress(double x, double y) {
        currSeg = new Line2D.Double(x,y,x,y);
        return currSeg;
    }

    @Override
    public void onRelease(double x, double y) {
        fireShapeDone(currSeg);
        currSeg = null;
    }

    // unused class for potential later use
    private class FreeShape implements Shape, Serializable {
        public static final long serialVersionUID = 129230349L;

        private final ArrayList<Shape> shapes = new ArrayList<Shape>();
        public void addShape(Shape s)
        {
            shapes.add(s);
        }
        
        public Rectangle getBounds() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public Rectangle2D getBounds2D() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public boolean contains(double x, double y) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public boolean contains(Point2D p) {
            synchronized (shapes)
            {
                for (Shape s : shapes)
                {
                    if (s.contains(p)) return true;
                }
            }
            return false;
        }

        public boolean intersects(double x, double y, double w, double h) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public boolean intersects(Rectangle2D r) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public boolean contains(double x, double y, double w, double h) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public boolean contains(Rectangle2D r) {
            synchronized (shapes)
            {
                for (Shape s : shapes)
                {
                    if (s.contains(r)) return true;
                }
            }
            return false;
        }

        public PathIterator getPathIterator(AffineTransform at) {
            PathIterator pi = new PathIterator() {

                public int getWindingRule() {
                    return PathIterator.WIND_EVEN_ODD;
                }

                public boolean isDone() {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void next() {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public int currentSegment(float[] coords) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public int currentSegment(double[] coords) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            };
            throw new UnsupportedOperationException("Not supported yet.");
        }

        public PathIterator getPathIterator(AffineTransform at, double flatness) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

    }
}
